package com.ktgj.train.service.impl;

import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;

import com.ktgj.common.core.annotation.Excel;
import com.ktgj.train.domain.TrainStationList;
import com.ktgj.train.domain.trainlist.CityList;
import com.ktgj.train.domain.trainlist.CityNameList;
import com.ktgj.train.domain.vo.CityNameListVo;
import com.ktgj.train.domain.vo.CityVo;
import com.ktgj.train.domain.vo.TrainTrainstationVo;
import net.sourceforge.pinyin4j.PinyinHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktgj.train.mapper.TrainTrainstationMapper;
import com.ktgj.train.domain.TrainTrainstation;
import com.ktgj.train.service.ITrainTrainstationService;

/**
 * 火车站信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-11-29
 */
@Service
public class TrainTrainstationServiceImpl implements ITrainTrainstationService 
{
    @Autowired
    private TrainTrainstationMapper trainTrainstationMapper;

    /**
     * 查询火车站信息
     * 
     * @param trainstationid 火车站信息ID
     * @return 火车站信息
     */
    @Override
    public TrainTrainstation selectTrainTrainstationById(Long trainstationid)
    {
        return trainTrainstationMapper.selectTrainTrainstationById(trainstationid);
    }

    /**
     * 查询火车站信息列表
     * 
     * @param trainTrainstation 火车站信息
     * @return 火车站信息
     */
    @Override
    public List<TrainTrainstation> selectTrainTrainstationList(TrainTrainstation trainTrainstation)
    {
        return trainTrainstationMapper.selectTrainTrainstationList(trainTrainstation);
    }

    /**
     * 新增火车站信息
     * 
     * @param trainTrainstation 火车站信息
     * @return 结果
     */
    @Override
    public int insertTrainTrainstation(TrainTrainstation trainTrainstation)
    {
        return trainTrainstationMapper.insertTrainTrainstation(trainTrainstation);
    }

    /**
     * 修改火车站信息
     * 
     * @param trainTrainstation 火车站信息
     * @return 结果
     */
    @Override
    public int updateTrainTrainstation(TrainTrainstation trainTrainstation)
    {
        return trainTrainstationMapper.updateTrainTrainstation(trainTrainstation);
    }

    /**
     * 批量删除火车站信息
     * 
     * @param trainstationids 需要删除的火车站信息ID
     * @return 结果
     */
    @Override
    public int deleteTrainTrainstationByIds(Long[] trainstationids)
    {
        return trainTrainstationMapper.deleteTrainTrainstationByIds(trainstationids);
    }

    /**
     * 删除火车站信息信息
     * 
     * @param trainstationid 火车站信息ID
     * @return 结果
     */
    @Override
    public int deleteTrainTrainstationById(Long trainstationid)
    {
        return trainTrainstationMapper.deleteTrainTrainstationById(trainstationid);
    }

    /**
     * 搜索
     * */
    @Override
    public List<TrainTrainstationVo> selectTrainTrainstationVague(String vagueName) {
        List<TrainTrainstation> trainstationList = trainTrainstationMapper.selectTrainstationVague("%"+vagueName+"%");
        List<TrainTrainstation> airportList = trainTrainstationMapper.selectAirportVague("%"+vagueName+"%");
        List<TrainTrainstationVo> trainTrainstationVoList = new CopyOnWriteArrayList<>();
        TrainTrainstationVo airportVo = new TrainTrainstationVo();
        String name = null;
        for (TrainTrainstation airport : airportList) {
            name = airport.getAirportcityname();
        }
        airportVo.setName(name);
        airportVo.setLabel("城市");
        trainTrainstationVoList.add(airportVo);

        for (TrainTrainstation trainTrainstation : trainstationList) {
            TrainTrainstationVo trainTrainstationVo = new TrainTrainstationVo();
            if (trainTrainstation.getIshot() == 1){
                trainTrainstationVo.setName(trainTrainstation.getTrainstationname()+"站");
                trainTrainstationVo.setLabel("火车站");
            }
            else {
                trainTrainstationVo.setName(trainTrainstation.getTrainstationname());
            }


            trainTrainstationVoList.add(trainTrainstationVo);
        }

        return trainTrainstationVoList;
    }

    @Override
    public List<TrainStationList> queryLikeByStation(String name) {
        return trainTrainstationMapper.queryLikeByStation(name);
    }

    @Override
    public List<CityList> queryCityList() {
        String[] letterList = {"热门", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
                "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
        List<CityList> cityLists = new ArrayList<>();
        List<CityNameListVo> cityNameLists = trainTrainstationMapper.queryCityList();
        if (cityNameLists.size() > 0) {
            for (int i = 0; i < letterList.length; ++i) {
                CityList cityList1 = new CityList();
                cityList1.setName(letterList[i]);
                List<CityNameList> cityNameList = new ArrayList<>();
                for (CityNameListVo nameList : cityNameLists) {
                    String pinyin = nameList.getTrainStationPinYin().substring(0, 1).toUpperCase();
                    if ((letterList[i].equals(pinyin)) || (letterList[i].equals("热门") && nameList.getIsHot() == 1)) {
                        CityNameList cityNameList1 = new CityNameList();
                        cityNameList1.setCityName(nameList.getCityName());
                        cityNameList1.setTrainStationPinYin(nameList.getTrainStationPinYin());
                        cityNameList1.setTrainStationJianPinYin(nameList.getTrainStationJianPinYin());
                        cityNameList1.setAirportCode(nameList.getAirportCode());
                        cityNameList.add(cityNameList1);
                    }
                }
                cityList1.setCityNameLists(cityNameList);
                if (cityList1.getCityNameLists().size() > 0) {
                    cityLists.add(cityList1);
                }
            }
        }
        return cityLists;
    }

    @Override
    public List<TrainStationList> queryIsHot() {
        return trainTrainstationMapper.queryIsHot();
    }

    /**
     * 城市列表
     * */
    @Override
    public List<CityVo> selectCityList() {
        List<CityVo> cityVoList = new ArrayList<>();
        List<TrainTrainstation> trainTrainstations = trainTrainstationMapper.selectCityList();
        Set<String> set = new HashSet<>();
        for (TrainTrainstation trainTrainstation : trainTrainstations) {
            if (trainTrainstation != null &&
                    trainTrainstation.getAirportcityname() != null && !trainTrainstation.getAirportcityname().equals("") &&
                    trainTrainstation.getAirportcode() != null && !trainTrainstation.getAirportcode().equals("")){

                if (trainTrainstation.getAirportcityname().equals("西双版纳")){
                    continue;
                }

                if (set.add(trainTrainstation.getAirportcityname())) {
                    CityVo cityVo = new CityVo();
                    cityVo.setCityName(trainTrainstation.getAirportcityname());
                    cityVo.setCityCode(trainTrainstation.getAirportcode());
                    cityVo.setCityId(trainTrainstation.getProvincecode());
                    cityVoList.add(cityVo);
                }
            }
        }
        return cityVoList;
    }

    /**
     * 获取火车站三字码
     * */
    @Override
    public TrainTrainstation selectCityId(String gaodename) {
        TrainTrainstation trainTrainstation = trainTrainstationMapper.selectCityId(gaodename);
        return trainTrainstation;
    }

    /**
     * 三字码获取城市名称
     * */
    @Override
    public TrainTrainstation selectCityName(String code) {
        TrainTrainstation trainTrainstation = trainTrainstationMapper.selectCityName(code);
        return trainTrainstation;
    }

    /**
     * 获取城市代码、id、名称
     * */
    @Override
    public CityVo cityVo(CityVo cityVo) {
        List<TrainTrainstation> trainTrainstation = trainTrainstationMapper.cityVo(cityVo);
        cityVo.setCityId(trainTrainstation.get(0).getProvincecode());
        cityVo.setCityCode(trainTrainstation.get(0).getAirportcode());
        cityVo.setCityName(trainTrainstation.get(0).getAirportcityname());
        return cityVo;
    }

    public static String getPinYinHeadChar(String str) {
        StringBuilder convert = new StringBuilder();
        for (int j = 0; j < str.length(); j++) {
            char word = str.charAt(j);
            String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
            if (pinyinArray != null) {
                convert.append(pinyinArray[0].charAt(0));
            } else {
                convert.append(word);
            }
        }
        return convert.toString().toUpperCase();
    }
}
